You can think of this section as a kind of "reverse index". Instead of stating what each program does, this index identifies a few things you might want to do, and tells you where in the sample programs you can find code which does what you want.

I want to control an output

Look in Exercise 1.1 for this. Remember that you'll need to configure TRISxas needed.

I want to read a switch or signal

Look in Exercise 2.1 for this.

I need a delay

There are a number of delay functions around the examples.Have a look in Exercise 1.7 . There is also one in utils.c which you can pick up easily. If you want a precise delay you should use a counter linked to an interrupt. Have a look in Exercise 9.4 at the code which times the Morse pulses.

I want to repeat something for ever

Use the while(1) construction you will find almost everywhere.

I want to do something if a pair of switches are set

You will need to use logical combinations to make this work. Have a look at Exercise 2.7 to see how I detect that both buttons are being held down.

I want to drive the LCD

Exercise 5.2 provides a base for using the LCD. If you want to take control at a lower level I'd advise you to add functions to this library. A set of programming commands for the LCD is given in this section.

I want to store values in the EEPROM

The file Exercise 7.1 will show you how to do this.

I want to make the processor sleep

The file Exercise 7.5 contains a sleeper function you can use in your program

I want to set up an interrupt on the timer

The file Exercise 4.4 contains code which sets the timer ticking.

I want to create interrupts at a specific rate

Remember that you can use the pre-scaler to get coarse control of the rate at which the timer expires. If you want very precise control of the timer you should make your interrupt load a new value in the counter for it to count up from. An example of how to compute this is given in Lab 4. The timers in the PIC microcontroller are very complex, so check the datasheets if you want to do anything advanced.

I want to drive the 7 segment LEDs

You can find an example of this in Exercise 4.4 . I would advise you to use interrupts for the display refresh.

I want to read a number

The file Exercise 7.3 contains a number reading function which will read integers up to the limits of the Variable.

I want to print a number

The Exercise 4.5 holds a decimal printing function for the LEDs

I want a random number

If you just want one random number, just update a counter while the user presses a button to select a command. This is very easy to implement and impossible to beat. If you want a sequence of numbers, take a look at the pseudo random code in Exercise 6.1 . If you want more complicated behaviour you could run two pseudo random sequences and then combine them. Alternatively you could make a LSFR using an integer (16 bit item) rather than a byte.

I want to select one option from a range

Use a switch construction for this. You can see one in action in Exercise 9.3 .

I want to look something up in a table

Arrays are good for this. Take a look at Exercise 9.3 for an example of how we search for the character which matches the pattern we received for our Morse letter.

I want my program to compile

If you get persistent compilation errors try moving lines around and see where the error goes. If you are missing a bracket or a semicolon the error may not be reported at the position it occurs. If the error persists try commenting out bits of code to make the error go away, and then restore the lines. The compiler will attempt to optimise your program by only compiling functions which it thinks are actually called. This can cause problems sometimes. Note that you might get loads of assembler warnings but these don't seem to affect the way the program runs.

I want my program to run

If your program doesn't do what you want, take a look at Exercise 6.3 and how I fixed it. If you can't figure out what is happening add some code to see what is going on. You can do this by printing to the LCD if you have one, or by driving the LEDs. Check that your if conditions are the right way round. If you use a function to configure the hardware make sure that you are calling it in main. I have wasted a lot of time looking for problems with non-working hardware when it has turned out to have been configured incorrectly or not at all. If you still can't work out why the program doesn't work, try backtracking to a version that does and reapplying the changes you've made one at a time. Also take a good look at the documentation in case the particular pin you are using works in a different way to all the others.

I want to use a different PIC microprocessor

All the PIC microcontrollers share a broadly compatible architecture, but there are a number of discrepancies. Make sure that you use the correct include file in the compiler (this is set as a compiler option). Also make sure that you have configured the extra hardware correctly.

I want to put assembler into my C source

You can do this easily using the asm preprocessor directive in XC8. Take a look at Exercise 5.1 (the LCD driver program) for an example of this.

I want to draw my own state diagrams

I used a product called "Rational Rose" to draw the state diagrams for this course. Rational Rose is a software design tool based around the Unified Modeling Language (UML). State diagrams are just one of a large number of views and diagram formats available with this tool. A demonstration version of this product is available from www.rational.com .